package com.example.springboot_mybatis.controller;

import com.example.springboot_mybatis.MybatisUtils;
import com.example.springboot_mybatis.bean.Student;
import com.example.springboot_mybatis.mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {

    @RequestMapping("/list")
    public List<Student> student_findall(){
        SqlSession session = MybatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        List<Student> stus = mapper.selectStudent();
        session.close();
        return stus ;
    }

    @RequestMapping("/find")
    public Student student_find(String id){
        SqlSession session = MybatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        Student stu = mapper.selectStudentOne(id);
        session.close();
        return stu ;
    }

    @RequestMapping("/delete")
    public void student_delete( String id){
        SqlSession session = MybatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        mapper.deleteStudent(id);
        session.commit();
        session.close();
    }

    @RequestMapping("/add")
    public void student_add(String id,String name,String birth,String sex){
        SqlSession session = MybatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        mapper.addStudent(id,name,birth,sex);
        session.commit();
        session.close();
    }


    @RequestMapping("/edit")
    public void student_edit(String id,String name,String birth,String sex){
        SqlSession session = MybatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        mapper.editStudent(id,name,birth,sex);
        session.commit();
        session.close();
    }

}
